package com.lgCore.cocurrent;/**
 * Created by by on 2020/11/13 10:35
 */

import com.lgCore.message.CommonRpcRequest;
import com.lgCore.netty.message.AskMsg;
import com.lgCore.netty.message.ReplyMsg;
import com.lgCore.netty.message.ReplyServerBody;
import com.lgCore.netty.server.NettyChannelMap;
import com.lgCore.server.factory.CommonRpcServiceFactory;
import com.lgCore.server.workHandler.invokBean.InvokRpcMethod;
import com.lgCore.util.ExceptionUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.concurrent.Callable;

/**
 * invok异步执行
 *
 * @program: sxreader_rpc
 * @author: ligang
 * @create: 2020-11-13 10:35
 */
public class AsgCallable implements Callable<ReplyServerBody> {
    private static final Logger LOGGER = LoggerFactory.getLogger(AsgCallable.class);


    private AskMsg askMsg;
    public AsgCallable(AskMsg askMsg) {
        this.askMsg = askMsg;
    }

    @Override
    public ReplyServerBody call() throws Exception {
        //收到客户端的请求
//                LOGGER.info("收到RPC客户端端信息：" + JSON.toJSONString(askMsg));
        try {
            //组装invok调用参数
            CommonRpcRequest commonRpcRequest = askMsg.getParams().getCommonRpcRequest();
            InvokRpcMethod invokRpcMethod = new InvokRpcMethod();
            invokRpcMethod.setMethod(commonRpcRequest.getMethodName());
            invokRpcMethod.setServicePath(commonRpcRequest.getServiceName());
            invokRpcMethod.setParamDatas(commonRpcRequest.getRequestObjects());
            invokRpcMethod = CommonRpcServiceFactory.getCommonRpcRouteService().getInvokRpcMethod(invokRpcMethod);
            //调用
            Object obj = null;
            Date date = new Date();
            ReplyServerBody replyBody = null;
            try {
                obj = CommonRpcServiceFactory.getCommonRpcRouteService().methodInvoke(invokRpcMethod);
//                        LOGGER.info("接口响应时间："+(new Date().getTime()-date.getTime()));
                replyBody = new ReplyServerBody(obj, 1);
            } catch (Exception e) {
                String errorMsg = ExceptionUtil.ExceptionDetailAll(e);
                replyBody = new ReplyServerBody(null, 0);
                replyBody.setExceptionMsg(errorMsg);
                LOGGER.info("接口调用失败：" + errorMsg);
                e.printStackTrace();
            }
            //多线程调用
//                        ExecutorService executorService = Executors.newCachedThreadPool();
//                        FutureTask<ReplyServerBody> futureTask = (FutureTask<ReplyServerBody>) executorService.submit(new ServiceInvokCallable(invokRpcMethod));
//                        ReplyServerBody replyBody = futureTask.get(RpcBaseCommonEntity.maxExecuteTime, TimeUnit.SECONDS);
            //结果发送给客户端
            ReplyMsg replyMsg = new ReplyMsg();
            replyMsg.setBody(replyBody);
            replyMsg.setBusinessId(askMsg.getBusinessId());
//                    LOGGER.info("发送给客户端信息："+JSON.toJSONString(replyMsg));
            NettyChannelMap.get(askMsg.getClientId()).writeAndFlush(replyMsg);


        } catch (Exception e) {
            e.printStackTrace();
            ReplyServerBody replyBody = new ReplyServerBody(null, 0);
            ReplyMsg replyMsg = new ReplyMsg();
            replyMsg.setBody(replyBody);
            replyMsg.setBusinessId(askMsg.getBusinessId());
            NettyChannelMap.get(askMsg.getClientId()).writeAndFlush(replyMsg);
        }
        return null;
    }
}
